package com.jieshuibao.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.bokecc.bean.LiveRoomStatus;
import com.jieshuibao.common.config.Const;
import com.jieshuibao.common.config.Global;
import com.jieshuibao.common.res.BaseRes;
import com.jieshuibao.model.db.LiveAdvisory;
import com.jieshuibao.model.db.LiveAdvisoryExample;
import com.jieshuibao.model.db.Liveroom;
import com.jieshuibao.model.db.LiveAdvisoryExample.Criteria;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.domain.LiveAdvisoryListRes;
import com.jieshuibao.model.domain.LiveAdvisoryReq;
import com.jieshuibao.model.domain.LiveAdvisoryRes;
import com.jieshuibao.service.BokeccService;
import com.jieshuibao.service.CheckUserOnlineService;
import com.jieshuibao.service.LiveAdvisoryService;
import com.jieshuibao.service.LiveroomService;
import com.jieshuibao.service.UserService;
import com.jieshuibao.util.DeleteHtmlUtil;
import com.jieshuibao.util.StringUtils;
import com.jieshuibao.util.UUIDGenerator;

@Controller
@RequestMapping(value = "/live")
public class LiveAdvisoryController {
	
	@Resource
	private LiveAdvisoryService liveAdvisoryService;
	
	@Resource
	private CheckUserOnlineService checkUserOnlineService;
	
	@Resource
	private LiveroomService liveroomService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private BokeccService bokeccService;
	
	/**
	 * 直播间咨询添加
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Object officialAdd(HttpServletRequest request,
			@RequestBody LiveAdvisoryReq req) {
		LiveAdvisoryRes res = new LiveAdvisoryRes();

		try {
			String cookieId = request.getHeader("cookieId");
			String online = checkUserOnlineService.isOnline(cookieId);
			if (online.equals(BaseRes.RELOGIN_CODE)) {
				res.setCode(BaseRes.RELOGIN_CODE);
				res.setDes(BaseRes.RELOGIN_DES);
				return res;
			}
			String question = req.getQuestion();
			if (StringUtils.isEmpty(question)) {
				res.setCode(BaseRes.ERRORPARAM_CODE);
				res.setDes("问题为空");
				return res;
			}
			User user = userService.getUserByPhoneNumber(online);

			if (user != null) {
				List<Liveroom> rooms = liveroomService.selectByExample(null);
				if (rooms!=null&&rooms.size()>0) {
					Liveroom liveroom = rooms.get(0);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("roomid", liveroom.getRoomId());
					map.put("name", liveroom.getName());
					map.put("cover", liveroom.getCover());
					LiveRoomStatus liveRoomStatus = bokeccService.liveRoomStatus(new String[] { liveroom.getRoomId() });
					if (null == liveRoomStatus) {
						res.setCode("0");
						res.setDes("获取直播间状态失败");
						return res;
					}
					int status = liveRoomStatus.getLiveStatus();
					if(status!=1){
						res.setCode("3");
						res.setDes("直播间已经结束");
						return res;
					}
					
				}
				LiveAdvisory liveAdvisory = new LiveAdvisory();
				String uuid = UUIDGenerator.getUUID();
				liveAdvisory.setUuid(uuid);
				liveAdvisory.setUserId(online);
				liveAdvisory.setDelFlag(Global.SHOW);
				Date date = new Date();
				liveAdvisory.setQtime(date);
				liveAdvisory.setUpdateTime(date);
				liveAdvisory.setQuestion(question);
				liveAdvisory.setRed("1");
				liveAdvisory.setStatus(Const.OFFICIAL_ADVISORY_WAIT);
				liveAdvisoryService.addLiveAdvisory(liveAdvisory);
				res.setTime(date);
				res.setUuid(uuid);
				res.setCode(BaseRes.SUCCESS_CODE);
				res.setDes("提交成功");
				return res;
			} else {
				res.setCode(BaseRes.ERRORPARAM_CODE);
				res.setDes("数据错误");
				return res;
			}

		} catch (Exception e) {
			e.printStackTrace();

			res.setCode(BaseRes.FAIL_CODE);
			res.setDes("网络抖动了");
			return res;

		}

	}
	/**
	 * 直播间咨询列表
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public Object officialList(HttpServletRequest request,
			@RequestBody LiveAdvisoryReq req) {
		LiveAdvisoryListRes res = new LiveAdvisoryListRes();

		try {
			String cookieId = request.getHeader("cookieId");
			String userId = checkUserOnlineService.isOnline(cookieId);
			if (userId.equals(BaseRes.RELOGIN_CODE)) {
				res.setCode(BaseRes.RELOGIN_CODE);
				res.setDes(BaseRes.RELOGIN_DES);
				return res;
			}
			//logger.error("接收到的索引："+mapper.writeValueAsString(req));
			int sinceId = req.getSinceId();
			int maxId = req.getMaxId();
			LiveAdvisoryExample liveAdvisoryExample = new LiveAdvisoryExample();
			liveAdvisoryExample.setPageIndex(sinceId - 1);
			liveAdvisoryExample.setPageSize(maxId - sinceId + 1);
			liveAdvisoryExample
					.setOrderByClause("live_advisory.updateTime desc");
			Criteria createCriteria = liveAdvisoryExample.createCriteria();
			createCriteria.andDelFlagEqualTo(Global.SHOW);
			createCriteria.andUserIdEqualTo(userId);
			List<LiveAdvisory> data = liveAdvisoryService.getPageLiveAdvisory(liveAdvisoryExample);
			
			for (LiveAdvisory liveAdvisory : data) {
				String answer = liveAdvisory.getAnswer();
				if (!StringUtils.isEmpty(answer)) {
					String textFromHtml = DeleteHtmlUtil
							.getTextFromHtml(answer);
					liveAdvisory.setAnswer(textFromHtml);
				}
				
			}
			res.setData(data);
			res.setSinceId(sinceId);
			res.setMaxId(maxId - 1 + data.size());
			res.setRows(data.size());
			res.setCode(BaseRes.SUCCESS_CODE);
			res.setDes(BaseRes.SUCCESS_DES);
			return res;

		} catch (Exception e) {
			e.printStackTrace();

			res.setCode(BaseRes.FAIL_CODE);
			res.setDes("网络抖动了");
			return res;

		}

	}
	
	@RequestMapping(value = "/details", method = RequestMethod.POST)
	@ResponseBody
	public  Object rofficialdetails(HttpServletRequest request,
			@RequestBody LiveAdvisoryReq req) {
			LiveAdvisoryRes res = new LiveAdvisoryRes();
			
		try {
			String uuid = req.getUuid();
			if(StringUtils.isEmpty(uuid)){
				res.setCode(BaseRes.ERRORPARAM_CODE);
				res.setDes("uuid为空");
				return res;
			}
			LiveAdvisory data = liveAdvisoryService.getLiveAdvisoryByUuid(uuid);
			if(data==null){
				res.setCode(BaseRes.ERRORPARAM_CODE);
				res.setDes("uuid错误");
				return res;
			}
			String red = data.getRed();
			if(red.equals("0")){
				LiveAdvisory liveAdvisory = new LiveAdvisory();
				liveAdvisory.setUuid(uuid);
				liveAdvisory.setRed("1");
				liveAdvisoryService.updateLiveAdvisory(liveAdvisory);
				liveAdvisoryService.updateLiveAdvisory(liveAdvisory);
				
			}
			res.setData(data);
			res.setCode(BaseRes.SUCCESS_CODE);	
			res.setDes(BaseRes.SUCCESS_DES);
			return res;
		
			} catch (Exception e) {
			e.printStackTrace();
			
			res.setCode(BaseRes.FAIL_CODE);
			res.setDes("网络抖动了");
			return res;
			
		}

	}

}
